Broup by测试

创建数据库,并写入数据

create database groupbyTest;
use groupbyTest;
create table r1 (a int);
insert into r1 values (1),(2),(1),(2),(1),(2),(1),(2),(1),(2),(1),(2),(1),(2);

简单查询

select * from r1;
select count(*) from r1;
select count(*) from r1 group by a;
select count(*) from r1 group by '1';

构造报错

由于 rand() 函数的存在,以下语句每次执行结果都不同

select left(rand(),3),a from r1 group by 1;

学习笔记/Hacker/SQL/SQL解析过程

引入 conunt() 函数,产生 group by 重复键冲突报错

select left(rand(),3),a,count(*) from r1 group by 1;

其他语句

-- round 最近的整数
select round(rand(),1),a,count(*) from r1 group by 1;
select a,count(*) from r1 group by round(rand(),1);
-- floor 向下取值,具有成功率
select floor(rand()*2),a,count(*) from r1 group by 1;